package com.joysuch.wwyt.datadump.biz.convertor;

import cn.zlg.common.datadump.annotation.DataDumpField;
import cn.zlg.common.datadump.convertor.FieldValueConvert;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 描述：日期格式
 *
 * @author zhangcancan
 * @date 2023/5/12 18:11
 **/
@Component
public class StringToDateConvertor implements FieldValueConvert<String, Date> {


    @Override
    public Date convert(Object bean, String value, DataDumpField dataField) {
        if (value == null) {
            return null;
        }

        try {
            return new SimpleDateFormat("yyyy-MM-dd").parse(value);
        } catch (ParseException e) {
            throw new IllegalArgumentException(value + " 日期格式非法,正确格式为[yyyy-MM-dd]");
        }
    }
}
